package com.sunq.studio.sys.service.impl;

import com.github.pagehelper.PageHelper;
import com.sunq.studio.sys.dao.TbUserDao;
import com.sunq.studio.sys.dao.TbUserLoginDao;
import com.sunq.studio.sys.domain.TbUser;
import com.sunq.studio.sys.domain.TbUserLogin;
import com.sunq.studio.sys.service.UserService;
import com.sunq.studio.utils.HttpHandler;
import com.sunq.studio.utils.WxConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by sunq on 2019/6/30.
 */
@Service
public class UserServiceImpl implements UserService {
    Logger logger= LoggerFactory.getLogger(ClassServiceImpl.class);

    @Autowired
    private TbUserDao tbUserDao;

    @Autowired
    private TbUserLoginDao tbUserLoginDao;

    @Override
    public TbUser getUser(Integer id) {
        TbUser user= tbUserDao.selectByPrimaryKey(id);
        return user;
    }

    @Override
    public List<TbUser> getUserbyLevel(int level,int page) {

        PageHelper.startPage(page,20);
        List<TbUser> list=tbUserDao.selectByVipLevel(level);
        return list;
    }

    @Override
    public List<TbUser> getTeacherList(int page) {
        PageHelper.startPage(page,20);
        List<TbUser> list=tbUserDao.getTeacherList();

        return list;
    }

    @Override
    public void insertUser(TbUser user) {

        tbUserDao.insertSelective(user);

        TbUser userlogin= tbUserDao.selectByOpenId(user.getWechatOpenid());

        userLoginRec(userlogin);
    }

    @Override
    public int updateUser(TbUser user) {

        int i=tbUserDao.updateByPrimaryKeySelective(user);
        return i;
    }

    @Override
    public TbUser userLogin(TbUser user) {

        if (user==null ||  (  StringUtils.isEmpty(user.getWechatOpenid())  )) {
            return null;
        }

        TbUser userlogin = tbUserDao.selectByOpenId(user.getWechatOpenid());



        //新登录用户
        if (userlogin == null) {
            user.setDelFlag(0);
            user.setLastDay(new Date());
            user.setFirstDay(new Date());
            user.setMoney(new BigDecimal(0.0));
            user.setSysAdmin(0);
            insertUser(user);
            userlogin = tbUserDao.selectByOpenId(user.getWechatOpenid());

        } else { //老用户
            userLoginRec(userlogin);
            TbUser user1=new TbUser();
            user1.setId(userlogin.getId());
            user1.setLastDay(new Date());
            tbUserDao.updateByPrimaryKeySelective(user1);
        }
        logger.info(MessageFormat.format("用户{0}登录了",userlogin.getNickname() ));
        return userlogin;
    }

    private  void userLoginRec(TbUser user){
        TbUserLogin login=new TbUserLogin();
        login.setUserId(user.getId());
        login.setLoginTime(new Date());
        login.setNickname(user.getNickname());
        login.setAvatarUrl(user.getAvatarUrl());
        tbUserLoginDao.insertSelective(login);

    }



    @Override
    public String dealUserWxInfo(String resCode){

         String  urls = "https://api.weixin.qq.com/sns/jscode2session?appid="+ WxConfig.appid + "&secret="+ WxConfig.secret + "&&js_code=" + resCode + "&grant_type=authorization_code";

        try {
            String res= HttpHandler.httpGet(urls);

            return  res;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }



}
